package com.starling.control.file
{
	import com.durej.PSDParser.PSDLayer;
	import com.durej.PSDParser.PSDParser;
	import com.starling.core.InfoDisplay;
	import com.starling.core.InfoImage;
	import com.starling.utils.TextureUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.System;
	import flash.utils.ByteArray;

	public class FilePSD extends FileBase
	{
		private var _urlLoader:URLLoader;
		
		public function FilePSD(path:String=null)
		{
			super(path);
		}
		
		
		/**
		 * 加载 
		 * 
		 */	
		override public function loadFile(callback:Function):void
		{
			super.loadFile(callback);
			
			dispose();
			fileItems = new Array();
			
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			_urlLoader.addEventListener(Event.COMPLETE ,loadComplete);
			_urlLoader.load(new URLRequest(this.nativePath));
		}
		
		
		/**
		 * 加载完成 
		 * 
		 */
		private function loadComplete(event:Event):void
		{
			try{
				var psd:PSDParser = PSDParser.getInstance();
				psd.parse(_urlLoader.data);
				
				for (var i:Number=0;i<psd.allLayers.length; i++) 
				{
					var psdLayer:PSDLayer = psd.allLayers[i];
					var layerBitmap_bmp:BitmapData = psdLayer.bmp;
					var layerBitmap:Bitmap = new Bitmap(layerBitmap_bmp);
					
					if(layerBitmap.width>0 && layerBitmap.height>0)
					{
						layerBitmap.name = psdLayer.name;
						layerBitmap.x = psdLayer.position.x;
						layerBitmap.y = psdLayer.position.y;
						layerBitmap.filters	= psdLayer.filters_arr;
						
						var textureName:String = layerBitmap.name;
						var textureType:String = TextureUtil.TYPE_IMAGE;
						
						fileItems.push({display:layerBitmap ,textureName:textureName ,textureType:textureType});
						//trace(textureName ,textureType);
					}
				}
				
				this._fileSize = this.size;
				this._fileNum = 1;
				this._callback();
			}catch(e:Error){
				Manage.mainView.text_file_info.text=e.message+"\n"+Manage.mainView.text_file_info.text;
			}
		}
		
		
		
		/**
		 * 显示对象数据 
		 * @param scale
		 * @return 
		 * 
		 */		
		override public function getInfo(scale:Number):Object
		{
			var info:Object = new Object();
			info[fileName] = new Array();
			
			for (var i:int=0;i<fileItems.length;i++)
			{
				var child:DisplayObject = fileItems[i].display;
				var infoObj:Object = InfoDisplay.getInfo(child ,scale);
				infoObj.textureName = fileItems[i].textureName;
				infoObj.textureType = fileItems[i].textureType;
				info[fileName].push(infoObj);
				
				info[infoObj.textureName] = InfoImage.getInfo(child ,scale);
			}
			
			return info;
		}
		
		
		
		
		
		/**
		 * 回收 
		 * 
		 */		
		override public function dispose():void
		{
			if(_urlLoader!=null)
			{
				_urlLoader.removeEventListener(Event.COMPLETE ,loadComplete);
				(_urlLoader.data as ByteArray).clear();
				_urlLoader = null;
			}
			
			if(fileItems!=null)
			{
				for (var i:int=0;i<fileItems.length;i++)
				{
					(fileItems[i].display as Bitmap).bitmapData.dispose();
					fileItems[i] = null;
				}
				fileItems = null;
			}
			
			System.gc();
		}
	}
}